package by.wind.drebedengi.datalayer;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import by.wind.drebedengi.datalayer.bean.Category;
import by.wind.drebedengi.datalayer.bean.Currency;
import by.wind.drebedengi.datalayer.bean.Place;
import by.wind.drebedengi.datalayer.bean.Record;
import by.wind.drebedengi.datalayer.bean.Source;

public class GetRecordList extends AbstractAction{

	public static final String SOAP_ACTION = "getRecordList";
	public static final String METHOD_NAME = "getRecordList";
	
	private ArrayList<Record> itsRecordList = new ArrayList<Record>();
	
	private ArrayList<Currency> itsCurrencyList = new ArrayList<Currency>();
	
	private ArrayList<Category> itsCategoryList = new ArrayList<Category>();
	
	private ArrayList<Place> itsPlaceList = new ArrayList<Place>();
	
	private ArrayList<Source> itsSourceList = new ArrayList<Source>();
	
	private void preLoadData() throws Exception{
		GetCurrencyList currList = new GetCurrencyList();
		currList.loadData();
		itsCurrencyList = currList.getCurrencyList();
		GetCategoryList catList = new GetCategoryList();
		catList.loadData();
		itsCategoryList = catList.getCategoryList();
		GetPlaceList placeList = new GetPlaceList();
		placeList.loadData();
		itsPlaceList = placeList.getPlaceList();
		GetSourceList sourceList = new GetSourceList();
		sourceList.loadData();
		itsSourceList = sourceList.getSourceList();
	}
	
	public void preLoadCurrencyData() throws Exception {
		GetCurrencyList currList = new GetCurrencyList();
		currList.loadData();
		itsCurrencyList = currList.getCurrencyList();
	}
	
	public void preLoadCategoryData() throws Exception {
		GetCategoryList catList = new GetCategoryList();
		catList.loadData();
		itsCategoryList = catList.getCategoryList();
	}
	
	public void preLoadPlaceData() throws Exception {
		GetPlaceList placeList = new GetPlaceList();
		placeList.loadData();
		itsPlaceList = placeList.getPlaceList();
	}
	
	public void preLoadSourceData() throws Exception {
		GetSourceList sourceList = new GetSourceList();
		sourceList.loadData();
		itsSourceList = sourceList.getSourceList();
	}
	
	public void preLoadRecordData() throws Exception {
		HashMap<String, Object> propertyList = new HashMap<String, Object>();
		propertyList.put(Constants.API_ID_KEY, Constants.API_ID_VALUE);
		propertyList.put(Constants.LOGIN_KEY, Constants.LOGIN_VALUE);
		propertyList.put(Constants.PASS_KEY, Constants.PASS_VALUE);
		
		Hashtable<String,Object> hashtable = new Hashtable<String,Object>();
        hashtable.put("is_report", Boolean.valueOf(false));
        hashtable.put("is_show_duty", Boolean.valueOf(true));
        hashtable.put("r_period", Integer.valueOf(/*6*/8)); //temporary get last 20 record
        hashtable.put("r_is_category", Integer.valueOf(0));
        hashtable.put("r_how", Integer.valueOf(1));
        hashtable.put("r_what", Integer.valueOf(6));
		
        propertyList.put(Constants.PARAMS, hashtable);
        Hashtable<Integer,Hashtable<String,String>> itsData = (Hashtable<Integer,Hashtable<String,String>>) loadData(METHOD_NAME, SOAP_ACTION, propertyList);
        if (itsData != null)
		{
        	Iterator<Map.Entry<Integer,Hashtable<String,String>>> iterator = itsData.entrySet().iterator();
        	while(iterator.hasNext())
        	{
        		Integer key = iterator.next().getKey();
        		Hashtable<String,String> element = itsData.get(key);
				itsRecordList.add(convertHashtableToRecord(element));
			}
		}
	}
	
	@Override
	public void loadData() throws Exception{
		preLoadCurrencyData();
		preLoadCategoryData();
		preLoadPlaceData();
		preLoadSourceData();
		preLoadRecordData();
		
	}
	
	public void loadData(Date dateFrom, Date dateTo) throws Exception {
		preLoadData();
		HashMap<String, Object> propertyList = new HashMap<String, Object>();
		propertyList.put(Constants.API_ID_KEY, Constants.API_ID_VALUE);
		propertyList.put(Constants.LOGIN_KEY, Constants.LOGIN_VALUE);
		propertyList.put(Constants.PASS_KEY, Constants.PASS_VALUE);
		
		Hashtable<String,Object> hashtable = new Hashtable<String,Object>();
		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
		hashtable.put("is_report", Boolean.valueOf(false));
        hashtable.put("is_show_duty", Boolean.valueOf(true));
        //hashtable.put("relative_date", "2012-11-02 23:59:59");
        hashtable.put("r_period", Integer.valueOf(0));
        hashtable.put("period_from", simpleFormat.format(dateFrom));
        hashtable.put("period_to", simpleFormat.format(dateTo));
        hashtable.put("r_how", Integer.valueOf(1));
        //hashtable.put("r_what", Integer.valueOf(2));
        
        propertyList.put(Constants.PARAMS, hashtable);
        
        Hashtable<Integer,Hashtable<String,String>> itsData = (Hashtable<Integer,Hashtable<String,String>>) loadData(METHOD_NAME, SOAP_ACTION, propertyList);
        if (itsData != null)
		{
        	Iterator<Map.Entry<Integer,Hashtable<String,String>>> iterator = itsData.entrySet().iterator();
        	while(iterator.hasNext())
        	{
        		Integer key = iterator.next().getKey();
        		Hashtable<String,String> element = itsData.get(key);
				itsRecordList.add(convertHashtableToRecord(element));
			}
		}
	}
	
	
	public Record convertHashtableToRecord(Hashtable<String,String> element) {
		Record recordElement = new Record();
				
		Iterator<Entry<String, String>> iterator = element.entrySet().iterator();
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_PARSER_STRING, Locale.getDefault());

		while(iterator.hasNext()) {
			
			String key = iterator.next().getKey();
			Object value = element.get(key);
			if (key.equals(Record.ID)) {
				recordElement.setServer_id(Long.parseLong((String) value));
			} else if (key.equals(Record.CURRENCY_ID)) {
				Currency curr = retrieveCurrencyById(Long.parseLong((String) value));
				recordElement.setCurrency(curr);
			} else if (key.equals(Record.OPERATION_DATE)) {
				try {
					recordElement.setOperation_date(sdf.parse((String)value));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else if (key.equals(Record.COMMENT)) {
				recordElement.setComment((String) value);
			} else if (key.equals(Record.SUM)) {
				recordElement.setSum(Long.parseLong((String) value));
			} else if (key.equals(Record.IS_DUTY)) {
				if (value.equals("t"))
					recordElement.setIs_duty(true);
			} else if (key.equals(Record.OPERATION_TYPE)) {
					recordElement.setOperation_type((Integer) value);
			} else if (key.equals(Record.PLACE_ID)) {
					recordElement.setPlace(retrievePlaceById(Long.parseLong((String) value)));
			} else if (key.equals(Record.BUDGET_OBJECT_ID)) {
					Category category = retrieveCategoryById(Long.parseLong((String) value));
					recordElement.setCategory(category);
					Source source = retrieveSourceById(Long.parseLong((String)value));
					recordElement.setSource(source);
			}
			
		}
		
		return recordElement;
	}
	
	public ArrayList<Record> getRecordList() {
		return itsRecordList;
	}
	
	public ArrayList<Currency> getCurrencyList() {
		return itsCurrencyList;
	}
	
	public ArrayList<Category> getCategoryList() {
		return itsCategoryList;
	}
	
	public ArrayList<Place> getPlaceList() {
		return itsPlaceList;
	}
	
	public ArrayList<Source> getSourceList() {
		return itsSourceList;
	}
	
	private Currency retrieveCurrencyById(long id) {
		for(Currency element : itsCurrencyList) {
			if (element.getServer_id() == id)
				return element;
		}
		return null;
	}
	
	private Category retrieveCategoryById(long id) {
		for(Category element: itsCategoryList) {
			if (element.getServer_id() == id)
				return element;
		}
		return null;
	}
	
	private Place retrievePlaceById(long id) {
		for(Place element : itsPlaceList) {
			if (element.getServer_id() == id)
				return element;
		}
		return null;
	}
	
	private Source retrieveSourceById(long id) {
		for(Source element : itsSourceList) {
			if (element.getServer_id() == id)
				return element;
		}
		return null;
	}
	
}
